﻿using DataScripter.Excel.Functions;

namespace DataScripter.Excel.Formulas
{
	class NextMonth : Formula
	{
		private Reference ReferenceToBaseCell { get; set; }
		private Formula NrOfMonthsToAdd { get; set; }

		public Formula Year { get; set; }
		public Formula Month { get; set; }
		public Formula Day { get; set; }

		public NextMonth(Reference referenceToBaseCell) : this(referenceToBaseCell, 1) 
		{}

		public NextMonth(Reference referenceToBaseCell, int nrOfMonthsToAdd) : this(referenceToBaseCell, new Value(nrOfMonthsToAdd))
		{}

		public NextMonth(Reference referenceToBaseCell, Formula nrOfMonthsToAdd)
		{
			ReferenceToBaseCell = referenceToBaseCell;
			NrOfMonthsToAdd = nrOfMonthsToAdd;
			RebaseYearOn(ReferenceToBaseCell);
			RebaseMonthOn(referenceToBaseCell);
			RebaseDayOn(ReferenceToBaseCell);
		}

		public void RebaseDayOn(Formula baseReference)
		{
			Day = DayFormula(baseReference);
		}

		public void RebaseMonthOn(Formula baseReference)
		{
			Month = MonthFormula(baseReference) + NrOfMonthsToAdd;
		}

		public void RebaseYearOn(Formula baseReference)
		{
			Year = YearFormula(baseReference);
		}

		public Formula DayFormula(Formula baseReference)
		{
			return new Day(baseReference);
		}
		public Formula MonthFormula(Formula baseReference)
		{
			return new Month(baseReference);
		}
		public Formula YearFormula(Formula baseReference)
		{
			return new Year(baseReference);
		}

		#region Overrides of Formula

		public override string Resolve(ICell current)
		{
			return string.Format(DutchFunctionNames.DATE + "({0};{1};{2})", Year.Resolve(current), Month.Resolve(current), Day.Resolve(current));
		}

		#endregion
	}
}
